package com.note.feng.stateMachine;

import com.note.feng.stateMachine.utils.ActionEnum;
import com.note.feng.stateMachine.utils.StateEnum;

import java.util.ArrayList;
import java.util.List;

public class StateMachine {

    /**
     * 存储状态信息
     */
    private List<StateTranaction> stateTranactionList;

    /**
     * 当前状态
     */
    private StateEnum currentState;

    public StateMachine(StateEnum currentState){
        this.currentState = currentState;
        stateTranactionList = new ArrayList<>();
    }

    /**
     * 添加一条状态信息
     * @return 状态信息
     */
    public StateTranaction addone(){
        StateTranaction stateTranaction = new StateTranaction();
        stateTranactionList.add(stateTranaction);
        return stateTranaction;
    }

    /**
     * 进行状态转移
     * @param action 动作
     * @return 状态信息
     */
    public StateTranaction execute(ActionEnum action){
        for (StateTranaction stateTranaction : stateTranactionList){
            if(currentState == stateTranaction.getCurrentState()
                    && action == stateTranaction.getAction()){
                stateTranaction.getEvent().handle();
                currentState = stateTranaction.getNextState();
                return stateTranaction;
            }
        }
        return null;
    }

}
